package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"orange/models"
	"time"
)

type OrangeController struct {
	beego.Controller
}
type Res struct {
	Code string
	Msg  string
	Data interface{}
}

func (c *OrangeController) Renlog() {
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = Res{"500", "参数不为空", nil}
		c.ServeJSON(true)
		return
	}
	user := models.User{
		UserName: username,
		PassWord: password,
	}
	err := models.DB.Debug().Create(&user).Error
	if err != nil {
		c.Data["json"] = Res{"500", "注册失败", nil}
		c.ServeJSON(true)

	} else {
		c.Data["json"] = Res{"200", "注册成功", user}
		c.ServeJSON(true)
	}

}
func (c *OrangeController) Renlogs() {
	username := c.GetString("username")
	password := c.GetString("password")
	if username == "" || password == "" {
		c.Data["json"] = Res{"500", "参数不为空", nil}
		c.ServeJSON(true)
		return
	}
	get := models.Client.Get("wangyashuo" + username)
	if get.Val() >= "3" {
		c.Data["json"] = Res{"500", "输入错误超过3次", nil}
		c.ServeJSON(true)
		return
	}
	var Clse models.User
	models.DB.Debug().Where("user_name = ?", username).Find(&Clse)
	if Clse.ID == 0 {
		incr := models.Client.Incr("wangyashuo" + username)
		if incr.Val() >= 3 {
			models.Client.Expire("wangyashuo", time.Hour*2)
			return
		}
		c.Data["json"] = Res{"500", "账号错误", nil}
		c.ServeJSON(true)
	}
	if Clse.PassWord != password {
		incr := models.Client.Incr("wangyashuo" + username)
		if incr.Val() >= 3 {
			models.Client.Expire("wangyashuo", time.Hour*2)
			return
		}
		c.Data["json"] = Res{"500", "密码错误", nil}
		c.ServeJSON(true)
	}
	c.Data["json"] = Res{"200", "登录成功", Clse}
	c.ServeJSON(true)
}
